package com.cancer.biz.auth.service.impl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.cancer.biz.auth.entity.msg.TextMessage;
import com.cancer.biz.auth.service.WechatService;
import com.cancer.common.util.WeixinUtil;
@Service
public class WechatServiceImpl implements WechatService {
    private final static Logger logger = LoggerFactory.getLogger(WechatServiceImpl.class);
    public String processRequest(HttpServletRequest request) {
        Map<String, String> map = WeixinUtil.xmlToMap(request);
        logger.info(map.toString());
        // 发送方帐号（一个OpenID）
        String fromUserName = map.get("FromUserName");
        // 开发者微信号
        String toUserName = map.get("ToUserName");
        // 消息类型
        String msgType = map.get("MsgType");
        
     // 默认回复一个"success"
        String responseMessage = "success";
        String respContent = "";
        TextMessage textMessage = new TextMessage();
        textMessage.setMsgType(WeixinUtil.MESSAGE_TEXT);
        textMessage.setToUserName(fromUserName);
        textMessage.setFromUserName(toUserName);
        textMessage.setCreateTime(System.currentTimeMillis());
        switch (msgType) {
	        //文本消息
			case WeixinUtil.MESSAGE_TEXT:
				textMessage(map,textMessage);
				break;
			//事件消息
			case WeixinUtil.MESSAGE_EVENT:
				// 事件类型
				String eventType = map.get("Event");
				switch (eventType) {
				case WeixinUtil.MESSAGE_EVENT_SUBSCRIBE:
					// 关注
					respContent = subscribe();
					break;
				case WeixinUtil.MESSAGE_EVENT_CLICK:
					// 事件KEY值，与创建自定义菜单时指定的KEY值对应
					respContent = click(map);
					break;
				case WeixinUtil.MESSAGE_EVENT_UNSUBSCRIBE:
					respContent = nuSubscribe();
					break;
				default:
					break;
				}
				textMessage.setContent(respContent);
			default:
				break;
		}
        responseMessage = TextMessage.textMessageToXml(textMessage);
        return responseMessage;

    }
    
    /**
	 * 文本消息
	 * @param params
	 * @param textMessage
	 * @return
	 */
	private TextMessage textMessage(Map<String, String> map,TextMessage textMessage){
		//内容
        String content=map.get("Content");
        if(content.equals("1")){
            textMessage.setContent("吃饭去");
        }else if(content.equals("2")){
            textMessage.setContent("喝水去");
        }else{
            textMessage.setContent("我去");
        }
        return textMessage;
	}
	/**
	 * 关注消息
	 * @param params
	 * @param textMessage
	 * @return
	 */
	private String subscribe(){
		return "感谢您的关注,这里会给您提供方便快捷服务！";
	}
	/**
	 * 取消关注消息
	 * @param params
	 * @param textMessage
	 * @return
	 */
	private String nuSubscribe(){
		return "取消关注！";
	}
	/**
	 * 菜单点击消息
	 * @param params
	 * @param textMessage
	 * @return
	 */
	private String click(Map<String, String> map){
		String respContent=null;
		String eventKey = map.get("EventKey");
		if("COMPANY_PROFILE".equals(eventKey)){
			respContent = "天津友让科技有限责任公司";
		}
		return respContent;
	}
}
